package ar.edu.unq.wannaplay.pages.abm.projects;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.unq.wannaplay.dtos.ProjectDTO;
import ar.edu.unq.wannaplay.dtos.SkillDTO;
import ar.edu.unq.wannaplay.employee.Experience;
import ar.edu.unq.wannaplay.pages.ProjectPage;
import ar.edu.unq.wannaplay.pages.abm.WPABM;
import ar.edu.unq.wannaplay.services.ProjectService;
import ar.edu.unq.wannaplay.tools.SkillSelector;

public class AddProjectPage extends WPABM<ProjectDTO, ProjectPage> {

    private static final long serialVersionUID = 1L;

    private List<SkillDTO> newSkills;

    @SpringBean(name = "service.projectService")
    private ProjectService projectService;

    public AddProjectPage() {
        super(ProjectPage.class, new ProjectDTO());
        this.init();
    }

    protected void init() {
    	newSkills = new ArrayList<SkillDTO>();
        WebMarkupContainer container = new WebMarkupContainer("data");
        this.makeData(container);
        this.makeSkillsTable(container);
        this.addButtons();
    }

    private void makeData(final WebMarkupContainer container) {
        container.add(new TextField<ProjectDTO>("name", new PropertyModel<ProjectDTO>(this.getAbmObject(), "name")));
        container.add(new TextField<ProjectDTO>("manHour",
                new PropertyModel<ProjectDTO>(this.getAbmObject(), "manHour")));
        container.add(new TextField<ProjectDTO>("clientName", new PropertyModel<ProjectDTO>(this.getAbmObject(),
                "clientName")));
        container.add(new TextField<ProjectDTO>("clientAddress", new PropertyModel<ProjectDTO>(this.getAbmObject(),
                "clientAddress")));
        container.add(new TextField<ProjectDTO>("clientMail", new PropertyModel<ProjectDTO>(this.getAbmObject(),
                "clientMail")));
        container.add(new TextField<ProjectDTO>("clientTown", new PropertyModel<ProjectDTO>(this.getAbmObject(),
                "clientTown")));
    }

    protected void makeSkillsTable(final WebMarkupContainer container) {
    	//skills que tengo
        container.add(new PageableListView<SkillDTO>("requirementsTable", this.getNewSkills(), 5) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<SkillDTO> item) {
                SkillDTO skill = item.getModelObject();
                item.add(new Label("skillName", skill.getKnowledge()));
                item.add(new Label("skillExp", skill.getExperience().toString()));
            }
        });

        //skills para agregar
        final SkillSelector skillSelector = new SkillSelector();
        container.add(new TextField<String>("newSkillName", new PropertyModel<String>(skillSelector, "name")));
        container.add(new DropDownChoice<Experience>("newSkillExp", new PropertyModel<Experience>(skillSelector,
                "experience"), skillSelector.getExperiences()));

        container.add(new Link<WebPage>("addSkill") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                AddProjectPage.this.getNewSkills().add(
                        new SkillDTO(skillSelector.getExperience(), skillSelector.getName()));
                AddProjectPage.this.makeSkillsTable(container);
            }
        });
    }

    @Override
    protected void accept() {
        this.getAbmObject().setRequirements(this.getNewSkills());
        this.getProjectService().save(this.getAbmObject());
    }

    public void setNewSkills(final List<SkillDTO> newSkills) {
        this.newSkills = newSkills;
    }

    public List<SkillDTO> getNewSkills() {
        return newSkills;
    }

    public void setProjectService(final ProjectService projectService) {
        this.projectService = projectService;
    }

    public ProjectService getProjectService() {
        return projectService;
    }

}
